package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐及其菜品关系
     * @param setmealDTO
     * @return
     */
    @CacheEvict(cacheNames = "setmealcache",key = "#setmealDTO.categoryId")//键setmealcache::#setmealDTO.categoryId
    @PostMapping
    @ApiOperation("增加套餐")
    public Result insertSetmeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.insertSetmeal(setmealDTO);
        return Result.success();

    }

    /**
     * 分页条件查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页条件查询")
    public Result<PageResult> pageSelect(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("新增菜品:分页条件查询:{}",setmealPageQueryDTO);
        PageResult pageResult=setmealService.pageSelect(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @CacheEvict(cacheNames = "setmealcache",allEntries = true)//清楚所有缓存数据
    @DeleteMapping
    @ApiOperation("删除套餐")
    public Result deleteSetmeal(@RequestParam List<Long> ids){
        log.info("删除的id:{}",ids);
        setmealService.deleteByIds(ids);
        return Result.success();

    }

    /**
     * 修改套餐-数据回显
     * @param id
     * @return
     */
    @CacheEvict(cacheNames = "setmealcache",allEntries = true)
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> selectSetmealByid(@PathVariable Long id){
        log.info("修改,根据id查询:{}",id);
        SetmealVO setmealVO =setmealService.selectSetmealBId(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @CacheEvict(cacheNames = "setmealcache",allEntries = true)
    @PutMapping
    @ApiOperation("修改套餐")
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改操作:修改的数据:{}",setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 更新套餐状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐状态更改")
    public Result statusManage(@PathVariable Integer status,Long id){
        log.info("套餐状态修改:状态:{},id:{}",status,id);
        setmealService.statusManage(status,id);
        return Result.success();

    }

}
